//
//  LDCollectionCellTow.m
//  Gift
//
//  Created by 李迪 on 15/11/10.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "LDCollectionCellTow.h"
#import "BaseHeader.h"
#import "MJRefresh.h"

@interface LDCollectionCellTow ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain)UITableView *tab;
@property (nonatomic, retain)NSMutableArray *array;
@property (nonatomic, assign)NSInteger page;


@end

@implementation LDCollectionCellTow

-(void)dealloc{
    
    [self.model release];
    [self.tab release];
    [self.dataStr release];
    [self.array release];
    [super dealloc];
}


- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
       
        [self createTab];
        self.array = [NSMutableArray array];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleChange:) name:@"lidi" object:nil];
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        NSString *passWord = [user objectForKey:@"night"];
        if ([passWord isEqualToString:@"yes"]) {
            self.tab.backgroundColor = BACKG_NIGHT;
        }
    }
    return self;
}

- (void)handleChange:(NSNotification *)Notification{
    
    NSString *passWord = [Notification object];
    if ([passWord isEqualToString:@"yes"]) {
        self.tab.backgroundColor = BACKG_NIGHT;
    }else if ([passWord isEqualToString:@"no"]){
        
        self.tab.backgroundColor = TITLE_NIGHT;
    }
    
}





- (void)setDataStr:(NSString *)dataStr{
    if (_dataStr != dataStr) {
        [_dataStr release];
        _dataStr = [dataStr retain];
    }
    self.page = 0;//默认请求下来的数据为第0页

    [self makeDataNumber:self.page];
    [self addHeader];
    [self addFooter];
}




#pragma mark - 处理数据
-(void)makeDataNumber:(NSInteger)number
{
    
    NSString *str = [NSString stringWithFormat:@"http://api.liwushuo.com/v2/channels/%@/items?ad=1&gender=1&generation=0&limit=20&offset=",self.dataStr];
    NSString *str_url = [NSString stringWithFormat:@"%@%ld",str,self.page];
    [PFAFNetworking getUrl:str_url body:nil response:PFJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
        
        if (number == 0) {
            self.array = [[NSMutableArray array]init];
        }

        for (NSDictionary *dic in [[resposeObject objectForKey:@"data"]objectForKey:@"items"]) {
            
            GiftModel *model = [GiftModel modelWithDic:dic];
            [self.array addObject:model];
        }
        
        [self.tab reloadData];
      
        [self.tab.header endRefreshing];
        [self.tab.footer endRefreshing];
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        [self.tab.header endRefreshing];
        [self.tab.footer endRefreshing];

    }];
    
}

//下拉刷新
-(void)addHeader
{
//    __block LDCollectionCellTow *vc = self;
    self.tab.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 0;
        [self makeDataNumber:self.page];
    }];
    
    [self.tab.header beginRefreshing];
}
//上拉加载
-(void)addFooter
{
//    __block LDCollectionCellTow *vc = self;
    self.tab.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        self.page = self.page + 20;
        [self makeDataNumber:self.page];
    }];

    [self.tab.footer beginRefreshing];
}







- (void)createTab{
    
    self.tab = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.contentView.frame.size.height) style:UITableViewStylePlain];
    
    
    [self.tab registerClass:[CellTow class] forCellReuseIdentifier:@"tabTow"];
    [self addSubview:self.tab];
    self.tab.rowHeight = SCREEN_HEIGHT / 4;
    self.tab.delegate = self;
    self.tab.dataSource = self;
    self.tab.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.tab release];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.array.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CellTow *cell = [tableView dequeueReusableCellWithIdentifier:@"tabTow"];
    GiftModel *model = [self.array objectAtIndex:indexPath.row];
    cell.model = model;
    return cell;
}

//3代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    GiftModel *model = [self.array objectAtIndex:indexPath.row];
    NSString *str = model.content_url;
    [self.delegate getPushNextPage:str Model:model];
}



@end
